from flask import Flask,render_template,request
import config
import os
from forms import RegisterForm,UploadForm
from werkzeug.datastructures import CombinedMultiDict

GAY_UPLOAD_PATH=os.path.join(os.path.dirname(__file__),'gay_images')
QINGNIANDA_UPLOAD_PATH=os.path.join(os.path.dirname(__file__),'qing_images')
app = Flask(__name__)
app.config.from_object(config)




@app.route('/')
def hello_world():
    return 'Hello World!'
@app.route('/register/',methods=['GET','POST'])
def register():
    if request.method=='GET':
        return render_template('upload1.html')
    else:
        form=RegisterForm(request.form)
        if form.validate():
            return 'sussess'
        else:
            print(form.errors)
            return 'fail'

    return render_template('upload1.html')
@app.route('/gaypeng/upload/',methods=['GET','POST'])
def upload():
    if request.method=='GET':
        return render_template('upload.html')
    else:
        form=UploadForm(CombinedMultiDict([request.form,request.files]))
        print(type(form))
        if form.validate():
            username=request.form.get('username')
            file=request.files.get('file')
            houzhui=os.path.splitext(file.filename)[1]
            filename=username+houzhui
            file.save(os.path.join(GAY_UPLOAD_PATH,filename))
            print(houzhui)
            print(username)
            return '文件上传成功'
        else:
            return '文件上传失败'

    return render_template('upload.html')
@app.route('/qingnianda/upload/',methods=['GET','POST'])
def upload1():
    if request.method=='GET':
        return render_template('upload1.html')
    else:
        form=UploadForm(CombinedMultiDict([request.form,request.files]))
        print(type(form))
        if form.validate():
            username=request.form.get('username')
            file=request.files.get('file')
            houzhui=os.path.splitext(file.filename)[1]
            filename=username+houzhui
            file.save(os.path.join(QINGNIANDA_UPLOAD_PATH,filename))
            print(houzhui)
            print(username)
            return '文件上传成功'
        else:
            return '文件上传失败'

    return render_template('upload1.html')

if __name__ == '__main__':
    app.run()
